# models.py
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
db = SQLAlchemy()


class User(db.Model):
	__tablename__ = 'user_info'
	id = db.Column(db.Integer, primary_key=True)
	account = db.Column(db.String(100), nullable=False, unique=True)
	password_hash = db.Column(db.String(100), nullable=False)  # 存储密码哈希
	email = db.Column(db.String(100), nullable=True)  # 假设email是可选的
	role = db.Column(db.String(100), nullable=True)  # 假设role是可选的
	sex = db.Column(db.String(100), nullable=True)
	phone_number = db.Column(db.String(100), nullable=True)
	
	def set_password(self, password):
		self.password_hash = generate_password_hash(password)  # 生成密码哈希
	
	def check_password(self, password):
		return check_password_hash(self.password_hash, password)  # 验证密码
	
	@staticmethod
	def create_user(account, password, email=None, role=None):
		new_user = User(account=account, email=email, role=role)
		new_user.set_password(password)  # 设置密码
		db.session.add(new_user)
		try:
			db.session.commit()
			return new_user
		except Exception as e:
			db.session.rollback()
			raise e


class PatientTable(db.Model):
	__tablename__ = 'patient_table'
	id = db.Column(db.Integer, primary_key=True)
	patient_id = db.Column(db.String(100), nullable=False)
	password = db.Column(db.String(100), nullable=False)
	patient_name = db.Column(db.String(100), nullable=False)
	patient_sex = db.Column(db.String(100), nullable=True)
	patient_age = db.Column(db.String(100), nullable=True)
	patient_height = db.Column(db.String(100), nullable=True)
	patient_weight = db.Column(db.String(100), nullable=True)
	patient_time = db.Column(db.DateTime(0), nullable=True)
	patient_department = db.Column(db.String(100), nullable=True)
	patient_type = db.Column(db.String(100), nullable=True)
	patient_hospital = db.Column(db.String(100), nullable=True)
	
	@staticmethod
	def create_from_csv_row(row):
		try:
			patient_id = str(row['病人编号']).strip()
			new_record = PatientTable(
				patient_id=patient_id,  # 确保转换为整数
				password=row['密码'],
				patient_name=row['名字'],
				patient_sex=row.get('性别'),
				patient_age=row.get('年龄'),
				patient_height=row.get('身高'),
				patient_weight=row.get('体重'),
				patient_time=row.get('患病时间'),
				patient_department=row.get('挂号科室'),
				patient_type=row.get('疾病类型'),
				patient_hospital=row.get('挂号医院')
			
			)
			print(f"Created new record: {new_record}")
			db.session.add(new_record)
			db.session.commit()
			print("Committed to database")
			return new_record
		except Exception as e:
			db.session.rollback()
			print(f"Error: {e}")
			raise e
	

class Message(db.Model):
	__tablename__ = 'messages'
	id = db.Column(db.Integer, primary_key=True)
	from_user = db.Column(db.String(80), nullable=False)  # 发送者用户名
	from_user_id = db.Column(db.Integer, nullable=False)  # 发送者用户ID
	to_user = db.Column(db.String(80), nullable=False)  # 接收者用户名
	to_user_id = db.Column(db.Integer, nullable=False)  # 接收者用户ID
	message = db.Column(db.Text, nullable=False)  # 留言内容
	type = db.Column(db.Integer, default=0)  # 留言类型 0代表管理员->病患, 1代表病患->管理员
	create_time = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间，日期时间类型，默认值为当前时间。
